/**
 * Created by charsen on 16/6/14.
 */
main.controller('priceMonitorCtrl', ["$scope", "$rootScope", "$location", "tools","$timeout", function ($scope, $rootScope, $location, tools,$timeout) {
    /*
    * init
    * */

    $('.priceMonitorContainer .dropdown').dropdown();




    $scope.searchAll={
        brandName:[],
        module:[],
        nick:[],
        plant:["淘宝","天猫","京东","1号店","苏宁","国美"],
        method:["满减","买就送","限时打折"]

    };

    function getNick(){
        $scope.searchAll.nick=[];
        tools.get({
            url:"/trend-spider/selectProduct.htm",
            data:{
                columns: "nick",
                groupBy: "nick",
                module:$scope.allModel[$scope.curIndex]
            },
            succ:function(resp){
                if(resp.success){
                    for(var i in resp.value){
                        $scope.searchAll.nick.push(resp.value[i].nick);
                    }
                    //console.log($scope.searchAll.nick);
                }else{
                    alert(resp.message);
                }
            }
        });
    }

    function getBrandName(){
        $scope.searchAll.brandName=[];
        tools.get({
            url:"/trend-spider/selectProduct.htm",
            data:{
                columns: "brand_Name",
                groupBy: "brand_Name",
                module:$scope.allModel[$scope.curIndex]

            },
            succ:function(resp){
                if(resp.success){
                    for(var i in resp.value){
                        $scope.searchAll.brandName.push(resp.value[i].brandName);
                    }
                    console.log($scope.searchAll.brandName);
                }else{
                    alert(resp.message);
                }
            }
        });
    }

    //获取型号
    $scope.allModel=[];
    function getModule(){
        tools.get({
            url:"/trend-spider/selectProduct.htm",
            data:{
                columns: "module",
                groupBy: "module"
            },
            succ:function(resp){
                if(resp.success){


                    for(var i in resp.value){
                        $scope.allModel.push(resp.value[i].module);
                    }
                    $rootScope.$broadcast("module_data_already_EV");


                }else{
                    alert(resp.message);
                }
            }
        });
    }

    $scope.curIndex=0;
    function init(){
        getModule();//获取所有的型号

        $rootScope.$on("module_data_already_EV",function(){
            getBrandName();
            getNick();
            $scope.showModels(0);
        });
        //根据型号查询
       /* getBrandName();
        getNick();*/
    }
    init();


/*枫fen叶*/


    $scope.numInPerPage = 14;
    //改变最多Perpage记录
    $scope.rowsChange = function(){
        console.log($("#changeRowsValue").val());
        var value=$("#changeRowsValue").val();
        var reg = new RegExp("^[0-9]*[1-9][0-9]*$");
        if(!reg.test(value)){
            $("#changeRowsValue").val($scope.numInPerPage);
        }else{
            $scope.numInPerPage = value;
        }
    };

    $scope.pageChanged = function(newPage) {
        getResultsPage(newPage);
    };


    $scope.totalSize = 0;
    $scope.pagination = {
        current: 1
    };
    //分页获取数据
    $scope.pros=[];
    function getResultsPage(pageNumber) {
        console.log({
            module: $scope.allModel[$scope.curIndex],
            begin: pageNumber-1,
            count: $scope.numInPerPage
        });
        if($scope.pagination.current==0){
            return;
        }
        tools.get({
            url:"/trend-spider/selectProduct.htm",
            data:{
                module: $scope.allModel[$scope.curIndex],
                begin: pageNumber-1,
                count: $scope.numInPerPage
            },
            succ:function(resp) {
                if (resp.success) {
                    $scope.pros=[];
                    for(var i in resp.value.products){
                        $scope.pros.push(resp.value.products[i]);
                        $scope.pros[i].platform=$scope.searchAll.plant[Math.floor(Math.random()*5)];
                    }
                    //$scope.pros = resp.value.products;

                  //  console.log($scope.pros);
                } else {
                    alert(resp.message);
                }
            }
        });
    }

    function getDataS(){
        /*if(){

        };*/
        tools.get({
            url:"/trend-spider/selectProduct.htm",
            data:{
                module: $scope.allModel[$scope.curIndex],
                begin: 0,
                count: 700

            },
            succ:function(resp) {
                if (resp.success) {
                    $scope.pros=[];
                    for(var i in resp.value.products){
                        $scope.pros.push(resp.value.products[i]);
                        $scope.pros[i].platform=$scope.searchAll.plant[Math.floor(Math.random()*6)];


                        $scope.pros[i].stangardPrice= ($scope.pros[i].price)+Math.floor(Math.random()*100)-50;

                        if($scope.pros[i].stangardPrice>$scope.pros[i].price){
                            $scope.pros[i].method = $scope.searchAll.method[Math.floor(Math.random()*3)];
                        }else if($scope.pros[i].stangardPrice=$scope.pros[i].price){
                            $scope.pros[i].method = "正常";
                        }else{
                            $scope.pros[i].method = "高价";
                        }


                       // $scope.pros[i].brandName=$scope.searchAll.brandName[Math.floor(Math.random()*3)];
                    }

                    $scope.totalSize = resp.value.totalSize;

                    //console.log($scope.pros);
                    $scope.pros2=angular.copy($scope.pros);

                } else {
                    alert(resp.message);
                }
            }
        });
    }

    //型号的展示-slider
    $scope.showModels = function(index){
        $scope.curIndex = index;
        $scope.pagination.current= 1;
        $scope.totalSize = 0;
        $scope.search = {};
        getDataS();
//        getResultsPage(1);
    };


    /*
     * 排序
     * */
    $scope.reverse = false;
    $scope.sortKey = 'id';
    $scope.sort = function(key){
        $scope.sortKey = key;
        $scope.reverse = !$scope.reverse;
    };

//添加价格监控
    $scope.addMonitor = function (index) {
        var title = null;
        if(index == 1){
            title = '添加产品监控';
        }else if(index == 2){
            title = '添加链接监控'
        }else if(index == 3){
            title = '添加品牌监控'
        }else if(index == 4){
            title = '添加店铺监控'
        }
        $rootScope.$broadcast('ADD_MONITOR_EV', {
            index: index,
            title: title,
            model: '',
            keywords: ['', '', ''],
            delkeywords: ['', ''],
            price_low: '',
            price_hig: '',
            time: ""
        });
    };
//  修改价格监控
    $scope.set = function(index,Typeindex,event){
        event.stopPropagation();
        event.preventDefault();
        console.log('set');
        var json={};

        if(Typeindex == 1){
            json.title = '修改型号监控';
        }else if(Typeindex == 2){
            json.title = '修改链接监控'
        }else if(Typeindex == 3){
            json.title = '修改品牌监控'
        }else if(Typeindex == 4){
            json.title = '修改店铺监控'
        }

        json.model = $scope.allModel[index];
        $rootScope.$broadcast('ADD_MONITOR_EV', {
            index: Typeindex,
            title: json.title,
            model: json.model,
            keywords: ['爽肤水', '清凉', '杭州专卖'],
            delkeywords: ['秋季', '婴儿'],
            price_low: '233',
            price_hig: '455',
            time: 5
        });
    };



    $scope.showAll=function(){
        $scope.pros=angular.copy($scope.pros2);
    };
    $scope.showExceed=function(){
        $scope.pros=angular.copy($scope.pros2);
        var arry=[];
        angular.forEach($scope.pros,function(item,index){

            if(Number(item.price-item.stangardPrice)!=0){

            }else{
                arry.push(index);
            }
        });
        for(var i=arry.length;i>0;i--){
            $scope.pros.splice(arry[i-1],1);
        }

    };
    $scope.showLow=function(){
        $scope.pros=angular.copy($scope.pros2);


        var arry=[];
        angular.forEach($scope.pros,function(item,index){
            if(Number(item.price-item.stangardPrice)<0){

            }else{
                arry.push(index);
            }
        });

        for(var i=arry.length;i>0;i--){
            $scope.pros.splice(arry[i-1],1);
        }

    };
    $scope.showHigh=function(){
        $scope.pros=angular.copy($scope.pros2);

        var arry=[];
        angular.forEach($scope.pros,function(item,index){
            if(Number(item.price-item.stangardPrice)>0){

            }else{
                arry.push(index);
            }
        });
        for(var i=arry.length;i>0;i--){
            $scope.pros.splice(arry[i-1],1);
        }
    };



//  查看图片
    $scope.lightPhoto = function(title,src){
        if(title=="低价截图"){
            $rootScope.$broadcast("lightPhoto_EV",{title:title,src:src,index:1});
        }else{
            $rootScope.$broadcast("lightPhoto_EV",{title:title,src:src,index:0});
        }

    };
/*------------------------------------------------链接监控---------------------------------------------------------------------*/

    //数据分析
    $scope.priceMonitorDataAnalyse = function () {
        $rootScope.$broadcast('PRICE_MONITOR_DATA_ANALYSE_EV');
    };



//    筛选
    $scope.search={};
    $('.dropdown')
        .dropdown({
            onChange: function(value, text, $selectedItem) {

               // console.log($selectedItem[0].attributes);
              //  console.log($selectedItem[0].attributes.value['value']);

                if($selectedItem[0].attributes.value==null){
                    return;
                }
                var sortValue = $selectedItem[0].attributes.value['value'];

           //     console.log("sortValue: "+sortValue+" text:"+text);


                if(text=="全部"||text=="平台"||text=="品牌"||text=="店铺"||text=="低价形式"){
                    $scope.search[sortValue]="";
                }else{
                    $scope.search[sortValue]= text;
                }

                $scope.$apply();
         //       console.log($scope.search);
                // custom action
            }
        });


    $scope.getPriceTrend = function (event,index) {

        var option=[];
        var title=$(event.target).attr('mytitle');
        var palnt=$(event.target).attr("plant");
      //  console.log($(event.target).attr("plant"));
        $rootScope.$broadcast('PRICE_TREND_EV',option,title,palnt,index);
    };














    $scope.model = [
        {
            hasNum:122,
            lowPriceNum:14,
            highPriceNum:50,
            name:'爽肤水 K-00945',
            docu:[
                {
                    pic: 'https://img.alicdn.com/bao/uploaded/i3/TB1S8J6JFXXXXX0XVXXXXXXXXXX_!!0-item_pic.jpg_430x430q90.jpg',
                    title: 'MAYSU/美素瑰蜜凝颜三件护理套装深度滋养补水保湿水乳护肤品套装',
                    platform: '天猫',
                    city: '杭州',
                    brand: 'MAYSU',
                    shopname: 'maysu美素旗舰店',
                    category: '化妆品',
                    stangardPrice: '389',
                    actualPrice: '189',
                    exceed: '0',
                    method: '无',
                    evid: '/trend-new-BI/assets/img/evid.png',
                    time: '13天17小时'
                },
                {
                    pic: 'https://g-search3.alicdn.com/img/bao/uploaded/i4/i3/TB1T1.yJFXXXXcdaXXXXXXXXXXX_!!0-item_pic.jpg_230x230.jpg',
                    title: '御泥坊亮彩柔肤黑面膜铁盒装补水保湿提亮肤色春夏护肤化妆品男女',
                    platform: '淘宝',
                    city: '宁波',
                    brand: '御泥坊',
                    shopname: '御泥坊旗舰店',
                    category: '化妆品',
                    stangardPrice: '159.9',
                    actualPrice: '149.9',
                    exceed: '-10',
                    method: '满减',
                    evid: '/trend-new-BI/assets/img/evid2.png',
                    time: '23天17小时'
                },
                {
                    pic: 'https://g-search1.alicdn.com/img/bao/uploaded/i4/i1/TB1YcCPKpXXXXXOXpXXXXXXXXXX_!!0-item_pic.jpg_230x230.jpg',
                    title: '水密码化妆品套装女士夏季补水深层保湿爽肤水乳液丹姿护肤品正品',
                    platform: '淘宝',
                    city: '广州',
                    brand: '水密码',
                    shopname: '水密码旗舰店',
                    category: '化妆品',
                    stangardPrice: '99-219',
                    actualPrice: '129-339',
                    exceed: '30-120',
                    method: '促销',
                    evid: '/trend-new-BI/assets/img/evid2.png',
                    time: '33天12小时'
                },
                {
                    pic: 'https://g-search2.alicdn.com/img/bao/uploaded/i4/i3/TB1Bk_wKpXXXXbxXpXXXXXXXXXX_!!0-item_pic.jpg_230x230.jpg',
                    title: '春纪化妆品套装 杨梅止渴套装 补水保湿 春夏季护肤 护肤品套装女',
                    platform: '天猫',
                    city: '杭州',
                    brand: 'MAYSU',
                    shopname: 'maysu美素旗舰店',
                    category: '化妆品',
                    stangardPrice: '389',
                    actualPrice: '490',
                    exceed: '103',
                    method: '满减',
                    evid: '/trend-new-BI/assets/img/evid.png',
                    time: '13天1小时'
                },
                {
                    pic: 'https://g-search2.alicdn.com/img/bao/uploaded/i4/i2/TB1BsFaLXXXXXcMXVXXXXXXXXXX_!!0-item_pic.jpg_230x230.jpg',
                    title: '迪士尼无毒儿童化妆品公主彩妆盒套装女孩玩具过家家玩具生日礼物',
                    platform: '淘宝',
                    city: '宁波',
                    brand: '御泥坊',
                    shopname: '御泥坊旗舰店',
                    category: '化妆品',
                    stangardPrice: '144.9',
                    actualPrice: '159.9',
                    exceed: '10',
                    method: '满减',
                    evid: '/trend-new-BI/assets/img/evid2.png',
                    time: '11天1小时'
                },
                {
                    pic: 'https://g-search1.alicdn.com/img/bao/uploaded/i4/i3/TB1dwplJFXXXXXEXXXXXXXXXXXX_!!0-item_pic.jpg_230x230.jpg',
                    title: '丸美套装正品专卖 巧克力丝滑护肤品套装 嫩白补水保湿化妆品套装',
                    platform: '淘宝',
                    city: '广州',
                    brand: '水密码',
                    shopname: '水密码旗舰店',
                    category: '化妆品',
                    stangardPrice: '299-219',
                    actualPrice: '129-339',
                    exceed: '30-120',
                    method: '促销',
                    evid: '/trend-new-BI/assets/img/evid2.png',
                    time: '3天17小时'
                },
                {
                    pic: 'https://img.alicdn.com/bao/uploaded/i3/TB1S8J6JFXXXXX0XVXXXXXXXXXX_!!0-item_pic.jpg_430x430q90.jpg',
                    title: 'MAYSU/美素瑰蜜凝颜三件护理套装深度滋养补水保湿水乳护肤品套装',
                    platform: '天猫',
                    city: '杭州',
                    brand: 'MAYSU',
                    shopname: 'maysu美素旗舰店',
                    category: '化妆品',
                    stangardPrice: '389',
                    actualPrice: '490',
                    exceed: '101',
                    method: '满减',
                    evid: '/trend-new-BI/assets/img/evid.png',
                    time: '13天17小时'
                },
                {
                    pic: 'https://g-search3.alicdn.com/img/bao/uploaded/i4/i3/TB1T1.yJFXXXXcdaXXXXXXXXXXX_!!0-item_pic.jpg_230x230.jpg',
                    title: '御泥坊亮彩柔肤黑面膜铁盒装补水保湿提亮肤色春夏护肤化妆品男女',
                    platform: '淘宝',
                    city: '宁波',
                    brand: '御泥坊',
                    shopname: '御泥坊旗舰店',
                    category: '化妆品',
                    stangardPrice: '149.9',
                    actualPrice: '159.9',
                    exceed: '10',
                    method: '满减',
                    evid: '/trend-new-BI/assets/img/evid2.png',
                    time: '23天17小时'
                },
                {
                    pic: 'https://g-search1.alicdn.com/img/bao/uploaded/i4/i1/TB1YcCPKpXXXXXOXpXXXXXXXXXX_!!0-item_pic.jpg_230x230.jpg',
                    title: '水密码化妆品套装女士夏季补水深层保湿爽肤水乳液丹姿护肤品正品',
                    platform: '淘宝',
                    city: '广州',
                    brand: '水密码',
                    shopname: '水密码旗舰店',
                    category: '化妆品',
                    stangardPrice: '99-219',
                    actualPrice: '129-339',
                    exceed: '30-120',
                    method: '促销',
                    evid: '/trend-new-BI/assets/img/evid2.png',
                    time: '33天12小时'
                },
                {
                    pic: 'https://g-search2.alicdn.com/img/bao/uploaded/i4/i3/TB1Bk_wKpXXXXbxXpXXXXXXXXXX_!!0-item_pic.jpg_230x230.jpg',
                    title: '春纪化妆品套装 杨梅止渴套装 补水保湿 春夏季护肤 护肤品套装女',
                    platform: '天猫',
                    city: '杭州',
                    brand: 'MAYSU',
                    shopname: 'maysu美素旗舰店',
                    category: '化妆品',
                    stangardPrice: '389',
                    actualPrice: '490',
                    exceed: '103',
                    method: '满减',
                    evid: '/trend-new-BI/assets/img/evid.png',
                    time: '13天1小时'
                },
                {
                    pic: 'https://g-search2.alicdn.com/img/bao/uploaded/i4/i2/TB1BsFaLXXXXXcMXVXXXXXXXXXX_!!0-item_pic.jpg_230x230.jpg',
                    title: '迪士尼无毒儿童化妆品公主彩妆盒套装女孩玩具过家家玩具生日礼物',
                    platform: '淘宝',
                    city: '宁波',
                    brand: '御泥坊',
                    shopname: '御泥坊旗舰店',
                    category: '化妆品',
                    stangardPrice: '144.9',
                    actualPrice: '159.9',
                    exceed: '10',
                    method: '满减',
                    evid: '/trend-new-BI/assets/img/evid2.png',
                    time: '11天1小时'
                },
                {
                    pic: 'https://g-search1.alicdn.com/img/bao/uploaded/i4/i3/TB1dwplJFXXXXXEXXXXXXXXXXXX_!!0-item_pic.jpg_230x230.jpg',
                    title: '丸美套装正品专卖 巧克力丝滑护肤品套装 嫩白补水保湿化妆品套装',
                    platform: '淘宝',
                    city: '广州',
                    brand: '水密码',
                    shopname: '水密码旗舰店',
                    category: '化妆品',
                    stangardPrice: '299-219',
                    actualPrice: '129-339',
                    exceed: '30-120',
                    method: '促销',
                    evid: '/trend-new-BI/assets/img/evid2.png',
                    time: '3天17小时'
                }
            ]
        }
    ];







    /*$('#container').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: '型号监控'
        },
        *//*subtitle: {
            text: 'Source: WorldClimate.com'
        },*//*
        xAxis: {
            categories: modelX
        },
        yAxis: {

            title: {
                text: '数量 (个)'
            }
        },
        tooltip: {
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                '<td style="padding:0"><b>{point.y:.1f} 个</b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        plotOptions: {
            column: {
                pointPadding: 0.2,
                borderWidth: 0
            }
        },
        credits: { enabled: false },
        series: [{
            name: '低价数',
            data: [49.9, -71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]

        }, {
            name: '高价数',
            data: [83.6, -78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3]

        }, {
            name: '超标量',
            data: [48.9, 38.8, 39.3, 41.4, -47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2]

        }]
    });
    $('#container2').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: '链接监控'
        },
       *//* subtitle: {
            text: 'Source: WorldClimate.com'
        },*//*
        xAxis: {
            categories: [
                'Jan',
                'Feb',
                'Mar',
                'Apr',
                'May',
                'Jun',
                'Jul',
                'Aug',
                'Sep',
                'Oct',
                'Nov',
                'Dec'
            ]
        },
        yAxis: {

            title: {
                text: '价格 (RMB)'
            }
        },
        tooltip: {
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        plotOptions: {
            column: {
                pointPadding: 0.2,
                borderWidth: 0
            }
        },
        credits: { enabled: false },
        series: [{
            name: '标准价',
            data: [49.9, -71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]

        }, {
            name: '实际价',
            data: [83.6, -78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3]

        }, {
            name: '超标量',
            data: [48.9, 38.8, 39.3, 41.4, -47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2]
        }]
    });




    $scope.download = function(){
        $('#tableID').tableExport({fileName:'tableID',type:'excel',escape:'true',consoleLog:'true' });
    };





    *//*
    * init
    * *//*

    //图表-highchat

*/

//




}]);